package DesignMode.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 观察者模式
 */
public class AllInOne {
    static class Subject{
        private List<Observers> list = new ArrayList<>();
        public void attach(Observers observer){
            list.add(observer);
        }

        public void detach(Observers observer) {
            list.remove(observer);
        }

        public void change() {
            list.forEach(item->{
                item.update();
            });
        }
    }

    static class Observers{
        public void update (){
            System.out.println("update!");
        }
    }

    public static void main(String []args) {
        Subject sub = new Subject();
        Observers obs = new Observers();
        sub.attach(obs);
        sub.change();
    }
}
